package com.py.framework.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.py.framework.common.controller.SystemController;
import com.py.framework.core.helper.HttpServletHelper;
import com.py.framework.core.log.ExceptionLogger;
import com.py.framework.core.rest.support.RestServiceResult;
import com.py.framework.service.AuthenticationService;

/**
 * 用户退出Controller
 * @author Leegern
 * @date   2018年5月3日
 */
@RestController
@RequestMapping("/hy")
public class LogoutController extends SystemController {
	
	@Autowired
	private AuthenticationService authenticationService;
	
	
	/**
	 * @api {post} /hy/logout.json 用户退出
	 * @apiName logout
	 * @apiVersion 0.0.1
	 * @apiGroup 用户退出 
	 * @apiParam {HttpServletRequest}  request 请求实例
	 * @apiParamExample {json} 请求参数示例
	 *  {}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":"","count":0,"data": {"flag":"true"}}
	 */
	@RequestMapping(value = "/logout", method = RequestMethod.POST)
	public RestServiceResult<Object> logout(HttpServletRequest request) {
		RestServiceResult<Object> result =super.getSuccessRestServiceResult();
		String jdToken = HttpServletHelper.getUserToken(request);
		if (StringUtils.isNotBlank(jdToken)) {
			try {
				authenticationService.clearUserSessionData(jdToken);
				result = super.getSuccessRestServiceResult();
			} catch (Exception e) {
				ExceptionLogger.error(e);
				result = super.getFailedRestServiceResult("用户退出失败！");
			}
		}
		return result;
	}
}